package com.bmps.springcloud.insuredmicroservice.service.amnt.strategy;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


public class Npt011AmntStrategy implements AmntStrategy{

    private static final Logger logger = LoggerFactory.getLogger(Npt011AmntStrategy.class);

    @Override
    public TrialPersonModel amntTrialCalculation(ProductModel productModel, RequestModel requestModel, RedisService redisService) {
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        for(InsurantModel insurantModel :insurantModelList) {
            if (insurantModel.getInsSeq() == productModel.getProdInsSeq()) {
                TrialPersonModel trialPersonModel = new TrialPersonModel();
                String riskCode = productModel.getProdCode();
                trialPersonModel.setRiskCode(riskCode);

                trialPersonModel.setPrem(productModel.getPrem());
                trialPersonModel.setPayEndYear(productModel.getPayYear());
                trialPersonModel.setInsuYear(productModel.getInsureYear());
                trialPersonModel.setAppAge(DateUtil.getYears(insurantModel.getBirthday(), requestModel.getPolicyInfo().getPolApplyDate()));
                trialPersonModel.setSex(insurantModel.getSex());
                trialPersonModel.setPayIntv(requestModel.getProductInfo().getPayIntv());

                StringBuilder sb = new StringBuilder(16);
                sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(riskCode);
                Object object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                TrialUtil.setSpecialElement(trialPersonModel, object.toString());

                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R006).append("|").append(trialPersonModel.getRiskCode()).
                        append("|").append(AcceptConstant.CALCULATION_TYPE_P);
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                trialPersonModel.setFormula(object.toString());
                logger.info("【" + productModel.getProdCode() + "】【公式】【KEY】" + sb.toString());

                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R001).append("|").append(riskCode);
                logger.info("【"+riskCode+"】【产品计算要素配置】【KEY】" + sb.toString());
                object = redisService.get(sb.toString());
                if(object == null){
                    return new TrialPersonModel();
                }
                String result = TrialUtil.getRateKey(trialPersonModel,object.toString(), AcceptConstant.CALCULATION_TYPE_P);
                object = redisService.get(result);
                if(object == null){
                    return new TrialPersonModel();
                }
                TrialUtil.setElementRate(trialPersonModel, object.toString());

                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R005).append("|").append(riskCode).
                        append("|").append(AcceptConstant.CALCULATION_TYPE_P);
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                logger.info("【"+riskCode+"】【产品计算公式要素配置】【KEY】"+sb.toString());
                TrialUtil.setElementMapping(trialPersonModel, object.toString());
                return trialPersonModel;
            }
        }
        return new TrialPersonModel();
    }

}
